
<template>
    <div class="threeJsPrc141">
      <div class="myCanvas" ref="myCanvas"></div>
    </div>
</template>

<script lang="ts">
import { Vue, Options } from 'vue-property-decorator';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
const scene = new THREE.Scene();
scene.add(new THREE.AxesHelper(250));
const point  = new THREE.PointLight(0xffffff);
point.position.set(400, 200, 300);
scene.add(point);
const ambient = new THREE.AmbientLight(0x444444);
scene.add(ambient);
const camera = new THREE.PerspectiveCamera();
camera.fov = 70;
camera.near = 1;
camera.near = 10000;
const renderer = new THREE.WebGLRenderer({
  alpha: true,
});
renderer.debug.checkShaderErrors = false;
renderer.setClearColor(0xb9d3ff, 1);

@Options({ components: {
} })
export default class ThreeJsPrc141 extends Vue {
  public rect: DOMRect|null=null;
  public dpr =750 / window.innerWidth;
  public controls!: OrbitControls;

  public sphereMesh(R: number, x: number, y: number, z: number) {
    const geometry = new THREE.SphereGeometry(R, 25, 25);
    const material = new THREE.MeshPhongMaterial({
      color: 0x0000ff,
    });
    const mesh = new THREE.Mesh(geometry, material);
    mesh.position.set(x, y, z);
    return mesh;
  }
  public boxMesh(width: number, height: number, depth: number, x: number, y: number, z: number) {
    const geometry = new THREE.BoxGeometry(width, height, depth);
    const material = new THREE.MeshPhongMaterial({
      color: 0x0000ff,
    });
    const mesh  = new THREE.Mesh(geometry, material);
    mesh.position.set(x, y, z);
    return mesh;
  }
  public mounted() {
    this.rect = (this.$el as HTMLElement).getBoundingClientRect();
    (this.$refs.myCanvas as HTMLDivElement).append(renderer.domElement);
    renderer.setPixelRatio(this.dpr);
    renderer.setSize(this.rect.width, this.rect.height, false);
    camera.aspect = this.rect.width / this.rect.height;
    camera.position.set(200, 100, 200);
    camera.lookAt(scene.position);
    renderer.render(scene, camera);

    const geometry = new THREE.BoxGeometry(100, 100, 100);
    const material = new THREE.MeshLambertMaterial({
      color: 0x0000ff,
    });
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

    const typeAPI = {
      MeshLambertMaterial: THREE.MeshLambertMaterial,
      MeshBasicMaterial: THREE.MeshBasicMaterial,
      MeshPhongMaterial: THREE.MeshPhongMaterial,
      PointsMaterial: THREE.PointsMaterial,
    };

    // 创建一个文件加载器，该加载器是对异步加载的封装
    const loader = new THREE.FileLoader();
    loader.load('material.json', (ele) => {
      console.log(ele);
      // const obj = JSON.parse(ele as string);
    });


    renderer.setAnimationLoop(() => {
      renderer.render(scene, camera);
    });

    this.controls = new OrbitControls(camera, renderer.domElement);
  }
}
</script>

<style lang="postcss" scoped>
.threeJsPrc141{
  touch-action: none;
  width: 100%;
  height: 100%;
  background: grey;
}
</style>
